package org.microdream.easy.deploy.utils;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by 老王 on 2018/3/1.
 * 时间工具类
 */
public class DateUtil {
	
	//静态指定时区
	static {
		TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
		TimeZone.setDefault(tz);
	}

    /**
     * Timestamp格式化显示 yyyy-MM-dd HH:mm
     */
    public static String TimestampToString_1(Timestamp timestamp){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return df.format(timestamp);
    }

    /**
     * Timestamp格式化显示 yyyy-MM-dd HH:mm:ss
     */
    public static String TimestampToString_3(Timestamp timestamp){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(timestamp);
    }


    /**
     * Timestamp格式化显示 yyyy-MM-dd
     */
    public static String TimestampToString_2(Timestamp timestamp){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(timestamp);
    }

    /**
     * Timestamp格式化显示 yyyyMMdd
     */
    public static String TimestampToString_4(Timestamp timestamp){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(timestamp);
    }
    /***
     *
     * @param date
     * @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDateByPattern(Date date, String dateFormat){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }

    /***
     * 把date转成cron表达式
     * @param date  : 时间点
     * @return
     */
    public static String getCron(Date  date){
        String dateFormat="ss mm HH dd MM ? yyyy";
        return formatDateByPattern(date, dateFormat);
    }

    /***
     * 把date转成cron表达式
     * @param time  : 时间点
     * @return
     */
    public static String getCron(Time time){

        String dateFormat="ss mm HH * * ?";
        return formatDateByPattern(time, dateFormat);
    }

    /***
     * 把timestamp转成cron表达式
     * @param tm  : 时间点
     * @return
     */
    public static String getCron(Timestamp tm){
        String dateFormat="ss mm HH dd MM ? yyyy";
        Date d = new Date(tm.getTime());
        return formatDateByPattern(d, dateFormat);
    }

    /***
     * 时间戳转换为date
     * @return
     */
    public static Date getDateTime(Timestamp timestamp){
        return new Date(timestamp.getTime());
    }

    /***
     * date转时间戳
     * @return
     */
    public static Date getTimestamp(Date date){
        return new Timestamp(date.getTime());
    }

    /***
     * String转时间戳
     * @return
     */
    public static Timestamp StringToTimestamp(String date){
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        try {
            ts = Timestamp.valueOf(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ts;
    }


    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 
     * @author 胡金鹏
     */
    public static boolean isEffectiveDate(Timestamp nowTime, Timestamp startTime, Timestamp endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据分钟数和当前时间比较
     * @param Minute
     * @return
     */
    public static  boolean compare(Integer Minute){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.MINUTE,Minute);
        long time = calendar.getTime().getTime();
        long time1 = new Date().getTime();
        if(time<=time1){
            return  true;
        }
        return  false;
    }

    /**
     * 获取当前年月日时
     * @return
     */
    public static String getTimeString(){
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH");
        return dateFormat.format(date);
    }
    
    /**
    * @Title:查询这个时间距离其当天凌晨的分钟数
    * @Author: 沈明可
    * @Param:
    * @return:
    * @CreateData: 2018/9/25  18:13
    **/
    public static Integer getBeforeDawn(Timestamp date){
        long time = date.getTime();
        SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd");
        try {
            long overTime = (time - (sdfOne.parse(sdfOne.format(time)).getTime()))/1000/60;
            return (int)overTime;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
    
    /**
    * @Title:计算时间距离现在相差多少分钟
    * @Author: 沈明可
    * @Param:
    * @return:
    * @CreateData: 2018/9/25  19:04
    **/
    public  static  Integer getMinute(Timestamp startTime){
        long time = new Date().getTime();
        long time1 = startTime.getTime();
        long l = (time - time1) / 1000 / 60;
        return  (int)l;
    }
    /**
     * @Title:将String类型的格式转换成LocalDateTime格式
     * @Author: jhy
     * @Param:
     * @return:
     * @CreateData: 2018/11/19 15:45
     **/
    public static LocalDateTime StringToLocalDateTime(String Time){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(Time,df);
        return ldt;
    }

    /**
     * 年月日转年月日时分秒
     * */
    public static String getDateByDate(String d) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(d);
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       return sdf.format(date);
    }
}
